import {Component, Input, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {Location} from '@angular/common';

import {User} from "./user"
import {UserService} from "./user.service"

@Component({
  selector: "top-nav",
  templateUrl: './top-nav.component.html',
  styleUrls: ["./top-nav.component.css"]
})
export class TopNavComponent implements OnInit {
  @Input() back: string;
  @Input() title: string;

  /*user: User*/

  user: User = new User()
  path: string
  name: string

  constructor(
    private userService: UserService,
    private location: Location,
    private router: Router) { }

  ngOnInit(): void {
    this.path = this.location.path()
    this.userService.getUserMine().then(user => {
      if (this.path != "/login" && !user){
        return this.router.navigate([ "login" ])
      }
      this.user = user
    })
  }

  backIndex(): void {
    this.router.navigate(["projects"])
  }

  goBack(): void {
    if (this.back)
      this.router.navigate([this.back]);
    else
      this.location.back();
  }

  login(): void {
    this.router.navigate(["login"])
  }

  logout(): void {
    localStorage.setItem("token", null)
    this.user = null
    UserService.MINE = null
    this.router.navigate([ "login" ])
  }
  /*
  isLogin(): void {
    if (!this.user){
        this.router.navigate(["login"])
    }
  }
  */
}
